package com.test.work;

/**
 * @author Cc
 * 2024-03-19
 */
public class CinemaThread extends Thread {
    /*
        公共属性 票数
     */
    private static int ticket = 2000;
    private int saleCount = 0;

    public static boolean ticketsAvailable() {
        return ticket > 0;
    }

    @Override
    public void run() {
        while (ticket >= 1) {
            synchronized (CinemaThread.class) {
                if (ticket > 0) {
                    ticket--;
                    saleCount++;
                    System.out.println(Thread.currentThread().getName() + ":" + "还剩" + ticket + "票");
                }
                if (saleCount >= 500)
                    Thread.yield();
            }
        }
        System.out.println(Thread.currentThread().getName() + "售出" + this.saleCount + "票");
    }
}
